package com.ccx.coachsite.task;

import org.json.JSONObject;

import android.content.Context;

import com.ccx.coachsite.bus.AccountMgr;
import com.ccx.coachsite.bus.SharedKey;
import com.ccx.coachsite.http.HttpCallBack;
import com.ccx.coachsite.http.HttpRequest;
import com.ccx.coachsite.http.UrlConfig;
import com.ccx.coachsite.ui.login.LoginHelper;
import com.ccx.coachsite.util.Log;
import com.ccx.coachsite.util.SharedPreferencesUtil;

public class LoginTask extends TaskBase {

	public LoginTask(HttpCallBack<?> iCall, Context context) {
		super(iCall, context);
		mTaskType = TaskType.TaskTypeLigin;
	}

	private String mobile;
	private String password;
	private int userType;

	public void setParams(String mobile, String password,int userType) {
		this.mobile = mobile;
		this.password = password;
		this.userType = userType;
	}
	
	@Override
	protected String genUrl(HttpRequest request)
	{
		String baseUrl = UrlConfig.getUrlByKey(UrlConfig.LoginUrlKey);
		baseUrl = baseUrl + "?mobile=" + mobile ;//+ "&password="+password;
		request.addParameter("password", password);
		//Log.e(baseUrl);
		return baseUrl;
	}
	
	protected void onDataParse(String responseString)
	{
		try {
			JSONObject jsonObject = new JSONObject(responseString);
			JSONObject respsonObject = jsonObject.getJSONObject("response");
			String user = respsonObject.getString("userType");
			boolean isExpert = false;
			if(user.equals("0"))
				isExpert = true;
			
			String userId = respsonObject.getInt("userId")+"";
			SharedPreferencesUtil.setBoolean(mContext, SharedKey.Shared_Is_Expert, isExpert);
			SharedPreferencesUtil.setValue(mContext,
					SharedKey.Shared_UserId_Key, respsonObject.getInt("userId")+"");
			
			if(userId != null && !userId.equals(""))
			{
				LoginHelper.doLoginStatic(mContext, mobile, userId);
			}
			
		} catch (Exception e) {
			Log.e(e.getMessage(), e);
		} finally {
		}
	}
}
